import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';
import './reducer/reducers.dart';
import './utils/jpush.dart';
void main() {
  Store<AppState> store = Store<AppState>(
      mainReducer,
      initialState: AppState(
        pushService: PushServiceState(),
        player: PlayerState()
      ));

  runApp(new MyApp(
    store: store,
  ));
}

class MyApp extends StatefulWidget {
  final Store<AppState> store;

  MyApp({Key key, this.store}) : super(key: key);
  @override
  _MyAppState createState() => new _MyAppState(store: this.store);
}

class _MyAppState extends State<MyApp> {
  final Store<AppState> store;
  PushUtil _push;
  _MyAppState({Key key, this.store});
  @override
  void initState() {
    super.initState();
    setState(() {
      _push = new PushUtil(this.store);
    });
  }

// 编写视图
  @override
  Widget build(BuildContext context) {
    return new StoreProvider(store: store, child: new MaterialApp(
      home: new StoreConnector<AppState, AppState> (
        builder: (BuildContext context,AppState state) {
          return new Scaffold(
            appBar: new AppBar(
              title: Text('JPUSH & Redux Demo')
            ),
            body: new Center(
              child: new ListView(children: [
              Text('regId: ${state.pushService.regId}'),
              Text('tags: ${state.pushService.tags}'),
              Text('screenInfo: ${state.player.screenInfo}'),
            ])),
          );
        },
        converter: (Store<AppState> store) {
          return store.state;
        }
      ),
      // routes: {
        // "login":(BuildContext context)=>new StoreConnector(
        //   builder: ( BuildContext context,Store<AppState> store ){
        //     return new LoginPage(callLogin: (String account,String pwd) async{
        //       print("正在登录，账号$account,密码:$pwd");
        //       // 为了模拟实际登录，这里等待一秒
        //       await new Async.Future.delayed(new Duration(milliseconds: 1000));
        //       if(pwd != "123456"){
        //         throw ("登录失败，密码必须是123456");
        //       }
        //       print("登录成功!");
        //       store.dispatch(new LoginSuccessAction(account: account));
        //     },);
        //   },
        //   converter: (Store<AppState> store){
        //     return store;
        //   }
        // ),
      // },

    ));
  }
}